<template>
  <div class="banner-container">
    <el-card>
      <el-form :inline="true"
               ref="search_data"
               class="demo-form-inline">
        <el-row>
          <el-form-item label="标题">
            <el-input v-model="search_data.keyword"
                      placeholder="输入标题内容" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary"
                       @click="onSearch">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never"
             style="margin-top:10px;">
      <div class="main-card">
        <div>
          <span style="font-size:18px;font-weight:900;">轮播图列表</span>
        </div>
        <div>
          <el-button icon="el-icon-plus"
                     type="primary"
                     @click="handleAdd()">新增</el-button>
        </div>
      </div>
      <el-table ref="multipleTable"
                :data="table_data"
                tooltip-effect="dark"
                style="width: 100%">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column prop="head"
                         label="图片"
                         width="300">
          <template slot-scope="scope">
            <el-image style="width: 200px; height: 85px"
                      :src="scope.row.head"
                      fit="contain"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title"
                         label="标题">
        </el-table-column>
        <el-table-column label="位置">
          <template slot-scope="scope">
            <div v-if="scope.row.type">首页</div>
            <div v-else>未知</div>
          </template>
        </el-table-column>
        <el-table-column label="产品">
          <template slot-scope="scope">
            <div>{{scope.row.with_product?scope.row.with_product.title:'未知'}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at"
                         label="创建时间">
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <el-tooltip class="item"
                        effect="dark"
                        content="编辑按钮"
                        placement="top-start">
              <i class="el-icon-edit"
                 @click="handleEdit(scope.row)"
                 style="margin-left:5px"></i>
            </el-tooltip>

            <el-tooltip class="item"
                        effect="dark"
                        content="删除按钮"
                        placement="top-start">
              <i class="el-icon-delete"
                 @click="handleDelete(scope.row.id,$index)"
                 style="margin-left:5px"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div style="padding: 10px 0; text-align: right;background:#fff;margin-top:10px">
      <el-pagination background
                     :page-size="search_data.limit"
                     layout="total, prev, pager, next"
                     :total="all_count"
                     @current-change="pageChange" />
    </div>
    <el-dialog title="新增/编辑公告"
               :visible.sync="dialogFormVisible"
               width="500px">
      <el-form :model="form"
               label-width="80px">
        <el-form-item label="图片">
          <el-upload class="upload-demo"
                     drag
                     :on-success="headUploadSuccess"
                     :action="upload_action"
                     :show-file-list="false">
            <img :src="form.head"
                 v-if="form.head"
                 style="width:100%;height:100%" />
            <i class="el-icon-upload"
               v-if="form.head==''"></i>
            <div class="el-upload__text"
                 v-if="form.head==''">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip"
                 slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="form.title"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="位置">
          <el-select v-model="form.type"
                     placeholder="请选择位置">
            <el-option :key="1"
                       label="首页"
                       :value="1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品">
          <el-select v-model="form.product_id"
                     placeholder="请选择商品">
            <el-option v-for="item in product_list"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="handleAddData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getList, addData, editData, delData } from '@/api/banner'
import { getList as getProductList } from '@/api/product'
export default {
  data () {
    return {
      search_data: {
        page: 1,
        limit: 15,
        keyword: '',
      },
      all_count: 0,
      upload_action: process.env.VUE_APP_UPLOAD_ACTION,
      table_data: [],
      dialogFormVisible: false,
      form: {
        id: '',
        type: '',
        title: '',
        head: '',
        product_id: '',
      },
      product_list: [],
      multiple_selection: [],
    }
  },
  created () {
    this.fetchData()
    this.fetchProductData()
  },
  methods: {

    pageChange (val) {
      this.search_data.page = val;
      this.fetchData();
    },
    fetchData () {
      getList(this.search_data).then(response => {
        this.table_data = response.data.data
        this.all_count = response.data.count
      })
    },

    fetchProductData () {
      getProductList().then(response => {
        if (response.code == 1000) {
          this.product_list = response.data.data
        }
      })
    },


    /**
     * 上传头图
     */
    headUploadSuccess (response, file, fileList) {
      this.form.head = response.data.data;
    },

    handleAdd () {
      this.form.id = null
      this.form.title = null
      this.form.type = null
      this.form.head = null
      this.form.product_id = null
      this.dialogFormVisible = true
    },

    handleAddData () {
      if (this.form.id) {
        editData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据保存成功',
              type: 'success'
            });
            this.dialogFormVisible = false
          } else {
            this.$message.error(response.msg);
          }
        })
      } else {
        addData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据添加成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.fetchData()
          } else {
            this.$message.error(response.msg);
          }
        })
      }
    },
    handleEdit (item) {
      this.form.id = item.id
      this.form.title = item.title
      this.form.type = item.type
      this.form.head = item.head
      this.form.product_id = item.product_id
      this.dialogFormVisible = true
    },


    handleDelete (id, index) {
      delData({ id: id }).then(response => {
        if (response.code == 1000) {
          this.table_data.splice(index, 1)
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.banner-container {
  max-width: 1300px;
  margin: 0 auto;
  padding: 30px;
  .main-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
